package com.mhxy.service.impl;

import com.mhxy.constant.PicConstants;
import com.mhxy.service.AbstractTask;
import com.mhxy.util.*;
import com.sun.jna.platform.win32.WinDef;
import lombok.extern.slf4j.Slf4j;

/**
 * 科举考试任务
 */
@Slf4j
public class ExamTaskImpl extends AbstractTask {

    public ExamTaskImpl(WinDef.HWND hwnd_0) {
        super(hwnd_0);
    }

    @Override
    public String getTaskName() throws Exception {
        return "科举考试";
    }

    @Override
    public Boolean before() throws Exception {
        clickEsc3();
        if (FuzzyPicUtil.findPic(hwnd_0, PicConstants.公共_活动, 2).isFinded()) {
            return true;
        }
        return goToJianYe();
    }

    @Override
    public Boolean execute() throws Exception {
        // 判断时间是否为17:00~24:00，日期是否为周一到周五
        if (!DateUtil.between(1, 5) || !TimeUtil.between(17, 23)) {
            log.info("【{}}】未在活动时间！", getTaskName());
            return false;
        }

        // 找到活动并打开
        if (!findAndClickActivity()) {
            return false;
        }

        // 找到三界奇缘并参加
        Boolean canYuRes = findAndClickTaskTitle(PicConstants.科举考试_任务图标标题, PicConstants.科举考试_完成图标);
        if (canYuRes == null || !canYuRes) {
            return canYuRes;
        }

        // 循环答题
        for (int i = 0; i < 15; i++) {
            FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.科举考试_答题A, 1);
            ThreadUtil.sleep1();
            if (i >= 10) {
                if (FuzzyPicUtil.findPicM(hwnd_0, new String[]{PicConstants.科举考试_结束标志, PicConstants.科举考试_结束标志2}, 1).isFinded()) {
                    break;
                }
            }
        }
        return true;
    }

    @Override
    public Boolean after() throws Exception {
        clickEsc3();
        clickUse(2);
        return true;
    }
}
